import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RoundInfo, RecmInfo } from "../ExecutePlan";
import { IRecmStrategy } from "../IRecmStrategy";
import { IPlan } from "../IExecutePlan";

export class WithSubPlanRecmStrategy implements IRecmStrategy {
    protected _subPlan: IPlan[] = [];

    constructor(name: string, subPlan: IPlan[]) {
        this._subPlan = subPlan;
    }

    setResult(roundInfo: RoundInfo, recmInfo: RecmInfo, multiple: number, waterRate: number, fuliRate: number): void {
        this._subPlan.forEach(plan => {
            plan.setResult(roundInfo, plan.recmInfo, plan.multiple, plan.waterRate, plan.fuliRate);
        })
    }
    toResult(roundInfo: RoundInfo): EnumBjlResult {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        this._subPlan.forEach(plan => {
            let rv = plan.toResult(roundInfo);
            let planRound = Object.assign({}, roundInfo);
            planRound.userRecm = rv;
            plan.userRecmInfo.recmV = rv;
            let [prv, prm] = plan.toMoney(planRound, plan.recmInfo, plan.userRecmInfo, plan.baseMoney);
            
            if (prv == EnumBjlResult.Z) {
                zm += prm;
            }
            else if (prv == EnumBjlResult.X) {
                xm += prm;
            }
        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
        }
        else if (zm < xm) {
            recmV = EnumBjlResult.X;
        }
        else {
            recmV = EnumBjlResult.U;
        }
        return recmV;
    }
    strategyName: string = "";
    debugCompoenent: string = "";
    resetRecm(): void {
        this._subPlan.forEach(plan => plan.resetRecm());
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as WithSubPlanRecmStrategy;
        if (obj) {
            this._subPlan.forEach((plan, idx) => {
                plan.copyValue(obj._subPlan[idx]);
            })
        }
    }

}